﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Modules.Hiner.Models.Order;
using Kaiser.Modules.Hiner.Models.Order.SearchModels;
using Kaiser.Modules.Hiner.Repositories;
using Kaiser.Modules.Hiner.Models.Product;
using Kaiser.Modules.Hiner.Models.Product.SearchModels;
using Kaiser.Models;
using System.Data;

namespace Kaiser.Repositories.iBatis.HINER
{
    public class OrderRepositorie : IOrderRepositorie
    {
        private IBatisHepler IBatisHepler = null;

        public OrderRepositorie()
        {
            IBatisHepler = new BatisHepler("SQLMap.Hinner.config");
        }




        public DataGrid<OrderBase> SearchOrder(OrderSearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<OrderBase>();

            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("SearchProductOrder", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        public bool OrderDelivery(string orderId)
        {
            OrderBase model = IBatisHepler.ExcuteSelectObj("GetProductOrder", orderId) as OrderBase;
            model.OrderStatus = 2;
            var result = IBatisHepler.ExcuteUpdate("UpdateProductOrder", model);
            return result > 0;
        }

        public void Insert(OrderBase model)
        {
            var result = IBatisHepler.ExcuteInsert("InsertProductOrder", model);
        }

        public void Update(OrderBase model)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateProductOrder", model);
        }

        public int Delete(object primaryId)
        {
            var result = IBatisHepler.ExcuteDelete("DeleteProductOrder", primaryId);
            return result;
        }

        public OrderBase Get(object primaryId)
        {

            OrderBase result = IBatisHepler.ExcuteSelectObj("GetProductOrder", primaryId) as OrderBase;
            return result;
        }

        public void DeleteOrderDetail(string orderID)
        {
            var result = IBatisHepler.ExcuteDelete("DeleteOrderDetail", orderID);
        }

        public void InsertOrderDetail(OrderDetail model)
        {
            var result = IBatisHepler.ExcuteInsert("InsertOrderDetail", model);
        }

        public IList<OrderDetail> GetOrderDetailList(string orderID)
        {

            var result = IBatisHepler.ExcuteSelectForList<OrderDetail>("GetOrderDetailList", orderID);
            return result;
        }




        public bool OrderConfirm(string orderId)
        {
            OrderBase model = IBatisHepler.ExcuteSelectObj("GetProductOrder", orderId) as OrderBase;
            model.OrderStatus = 1;
            var result = IBatisHepler.ExcuteUpdate("UpdateProductOrder", model);
            return result > 0;
        }


        public int DeleteList(IList<string> ids)
        {
            throw new NotImplementedException();
        }
    }
}
